/// @tags:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>

namespace BlueQuantum {

int const N = 3e5 + 5, M = 2e6 + 5;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

int n, cnt = 1, tot;
char str[N];
int k[N], rt[N], ans[N], ls[M], rs[M], sum[M], tr[N][26];
std::vector<int> q[N];

void insert(int &x, int l, int r, int val) {
  if (!x) x = ++tot;
  if (l == r) { return sum[x]++, void(); }
  int mid = (l + r) >> 1;
  if (val <= mid) insert(ls[x], l, mid, val);
  else
    insert(rs[x], mid + 1, r, val);
  sum[x] = sum[ls[x]] + sum[rs[x]];
}

void merge(int &x, int y) {
  if (!(x && y)) return x |= y, void();
  merge(ls[x], ls[y]);
  merge(rs[x], rs[y]);
  sum[x] = sum[ls[x]] + sum[rs[x]];
}

int query(int x, int l, int r, int k) {
  if (k > sum[x]) return -1;
  if (l == r) return l;
  int mid = (l + r) >> 1;
  if (sum[ls[x]] >= k) return query(ls[x], l, mid, k);
  else
    return query(rs[x], mid + 1, r, k - sum[ls[x]]);
}

inline void insert(int id) {
  int cur = 1;
  int m = strlen(str + 1);
  for (int i = m; i; --i) {
    int c = str[i] - 'a';
    if (!tr[cur][c]) tr[cur][c] = ++cnt;
    cur = tr[cur][c];
  }
  q[cur].push_back(id);
  insert(rt[cur], 1, n, id);
}

void solve(int now) {
  for (int i = 0; i < 26; i++)
    if (tr[now][i]) {
      solve(tr[now][i]);
      merge(rt[now], rt[tr[now][i]]);
    }
  for (std::vector<int>::iterator i = q[now].begin(); i != q[now].end(); ++i) {
    ans[*i] = query(rt[now], 1, n, k[*i]);
  }
}

inline int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + 1);
    insert(i);
  }
  for (int i = 1; i <= n; i++) read(k[i]);
  solve(1);
  for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("sam.in", "r", stdin);
  freopen("sam.out", "w", stdout);
#endif
#endif

  return BlueQuantum::main();
}